<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('productCenter.appConfig.versionUpdate.type')">
          <el-select v-model="searchForm.forceUpdate" clearable :placeholder="$t('productCenter.appConfig.versionUpdate.pleaseType')" style="width: 100%">
            <el-option
              v-for="item in $t('productCenter.appConfig.versionUpdate.updateChoose')"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="primary" @click="addItem">
        {{ $t('productCenter.appConfig.versionUpdate.add') }}
      </el-button>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;margin-top: 10px"
      >
        <el-table-column
          prop="id"
          :label="tableText['id']"
          min-width="80"
        />
        <el-table-column
          prop="newVersion"
          :label="tableText['updatedVersion']"
          min-width="100"
        />
        <el-table-column
          prop="forceUpdate"
          :label="tableText['updatedType']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row['forceUpdate'] ? $t('productCenter.appConfig.versionUpdate.updateChoose')[1]['name'] : $t('productCenter.appConfig.versionUpdate.updateChoose')[0]['name'] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="updateLog"
          :label="tableText['updatedContent']"
          min-width="350"
        />
        <el-table-column
          prop="googleUrl"
          :label="tableText['googleLink']"
          min-width="200"
        />
        <el-table-column
          prop="serverUrl"
          :label="tableText['serverLink']"
          min-width="150"
        />
        <el-table-column
          prop="createTimeStr"
          :label="tableText['createTime']"
          min-width="160"
        />
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="100"
        >
          <template slot-scope="scope">
            <el-button size="small" @click="deleteItem(scope.row)">{{ $t('common.operate.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <version-update-dialog
      :show-dialog="editDialog"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
    <delete-dialog
      ref="delete-dialog"
      :delete-state="deleteState"
      :loading="deleteLoading"
      @change-delete="changeDelete"
      @delete-this="deleteThis"
    />
  </el-row>
</template>

<script>
import { getVersion, deleteVersion } from '@/api/product'
import VersionUpdateDialog from '@/components/product/version-update-dialog'
import DeleteDialog from '@/components/delete/index.vue'
export default {
  name: 'VersionUpdate',
  components: {
    VersionUpdateDialog,
    DeleteDialog
  },
  data() {
    return {
      loading: false,
      editDialog: false,
      deleteState: false,
      deleteLoading: false,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      id: null,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('productCenter.appConfig.versionUpdate')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        forceUpdate: null
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.getData()
    },
    getData() {
      this.loading = true
      getVersion(this.searchForm)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    addItem() {
      this.editDialog = true
    },
    editSuccess() {
      this.getData()
    },
    changeDialog(param) {
      this.editDialog = param
    },
    deleteItem(data) {
      this.id = data.id
      this.changeDelete(true)
    },
    changeDelete(param) {
      this.deleteState = param
    },
    deleteThis() {
      this.deleteLoading = true
      const id = this.id
      deleteVersion(id)
        .then(res => {
          this.deleteLoading = false
          if (res.success) {
            this.$message.success(this.$t('common.tip.deleteSuccess'))
            this.changeDelete(false)
            this.getData()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.deleteLoading = false
          this.$message.error(err.message)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
</style>
